$(document).ready(function () {
    $.plugin = {
        NUMBER_plus : function (obj,set_num,speed) {
            if (!speed)
                speed = 60;
            if (!set_num)
                set_num = 50;
            var scroll_lock = true;
            $(window).scroll(function () {
                if ($(document).scrollTop() + $(window).height() > obj.offset().top && obj.offset().top+obj.innerHeight() > $(document).scrollTop() && scroll_lock) {
                    scroll_lock = false;
                    var time_lock = 0,
                        This = obj.find('li'), // 获取对象
                        num = [];  // 数据容器
                    This.each(function () {
                        //数据缓存
                        num.push($(this).find('em').attr('data-num')*1);
                    });
                    var set_plus_num = function(index){
                            // 数字相加
                            if (This.eq(index).find('em').text()*1 > num[index]-num[index]/set_num){
                                This.eq(index).find('em').text(parseInt(num[index]));
                            }else {
                                This.eq(index).find('em').text(parseInt(num[index]/set_num + This.eq(index).find('em').text()*1));
                            }
                            //较小数字处理
                            if (num[index] < set_num){
                                This.eq(index).find('em').text(parseInt(num[index]));
                            }
                        },
                        set_plus = setInterval(function () {
                            // 动画执行
                            for (var i=0;i<=This.length;i++){
                                if (num[i-1] > This.eq(i-1).find('em').text()*1) {
                                    set_plus_num(i-1);
                                    if (num[i-1] === This.eq(i-1).find('em').text()*1){
                                        time_lock++;
                                    }
                                }
                            }
                            if (time_lock >= num.length){
                                // 清理多余执行
                                clearInterval(set_plus);
                            }
                        },speed);
                }
            })
        },
        BG_parallax : function (obj, speed) {
            if (!speed)
                speed = .5;
            $(window).scroll(function () {
                obj.css({
                    'background-position-y': (((obj.offset().top + obj.innerHeight()/2) - ($(document).scrollTop() + $(window).height()/2)) * speed)
                })
            })
        },
        Interval_Fun : function (obj,means,cycle,position) {
            if (!position)
                position = 2;
            if (!cycle)
                var cycle_num = 0;
            $(window).scroll(function () {
                if (obj.offset().top + obj.innerHeight()/position < $(document).scrollTop()+$(window).height() && obj.offset().top + obj.innerHeight()/position > $(document).scrollTop()){
                    cycle_num++;
                    if (!cycle && cycle_num <= 1)
                        means();
                    else if (cycle)
                        means()
                }
            })
        },
        Video_open : function (obj) {
            var src,btn,img,tag,method = obj.attr('data-mode');
            if (obj.attr('data-src'))
                src = obj.attr('data-src');
            else
                return;
            if (obj.attr('data-img'))
                img = obj.attr('data-img');
            if (obj.attr('data-btn'))
                btn = obj.attr('data-btn') ? obj.attr('data-btn') : '#004dda';
            if (!method)
                tag = '<video controls poster='+ img +' src='+ src +'></video>';
            else
                tag = '<iframe width="560" height="315" src="'+src+'" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>';
            var video = "<div class='video_box active'>"+tag+"<i class='video_close fa fa-close'></i></div><i class='video_mask active'></i>",
                css = "<style type='text/css' id='video_css'>.video_box {position: fixed;display: inline-block;top: 50%;left: 50%;-webkit-transform: translate(-50%, -50%) scale(1, 1);-moz-transform: translate(-50%, -50%) scale(1, 1);-ms-transform: translate(-50%, -50%) scale(1, 1);-o-transform: translate(-50%, -50%) scale(1, 1);transform: translate(-50%, -50%) scale(1, 1);z-index: 1000;max-width: 90%; max-height: 90%;-webkit-transition: .5s;-moz-transition: .5s;-ms-transition: .5s;-o-transition: .5s;transition: .5s;-webkit-transform-origin: center center;-moz-transform-origin: center center;-ms-transform-origin: center center;-o-transform-origin: center center;transform-origin: center center;zoom: 1;}.video_box video,.video_box iframe {max-width: 100%;max-height: 100%;-webkit-transition: .5s;-moz-transition: .5s;-ms-transition: .5s;-o-transition: .5s;transition: .5s;}.video_box i.video_close {position: absolute;height: 40px;width: 40px;border-radius: 100%;background: "+btn+";display: block;right: -20px;top: -20px;cursor: pointer;cursor: hand;text-align: center;line-height: 40px;color: white;font-size: 18px;opacity: 0;-webkit-transition: .5s;-moz-transition: .5s;-ms-transition: .5s;-o-transition: .5s;transition: .5s;}.video_box:hover i.video_close {opacity: 1;}.video_box.active {-webkit-transform: translate(-50%, -50%) scale(0.3, 0.3);-moz-transform: translate(-50%, -50%) scale(0.3, 0.3);-ms-transform: translate(-50%, -50%) scale(0.3, 0.3);-o-transform: translate(-50%, -50%) scale(0.3, 0.3);transform: translate(-50%, -50%) scale(0.3, 0.3);zoom: .3;}i.video_mask {position: fixed;top: 0;left: 0;z-index: 900;background: rgba(0, 0, 0, 0.5);height: 100%;width: 100%;-webkit-transition: .5s .2s;-moz-transition: .5s .2s;-ms-transition: .5s .2s;-o-transition: .5s .2s;transition: .5s .2s;opacity: 1;}i.video_mask.active {opacity: 0;-webkit-transition: .5s;-moz-transition: .5s;-ms-transition: .5s;-o-transition: .5s;transition: .5s;}</style>"
            obj.click(function () {
                $("body").append(video).append(css).animate(200,function () {$(".video_box").add($(".video_mask")).removeClass('active');});
                $("i.video_mask").add($("i.video_close")).one('click',function () {$(".video_box").add($("i.video_mask")).add($("#video_css")).remove();})
            });
        },
        Line_curve:function (obj,origin,coordinate,color,speed) {
            if (!speed)
                speed = 0.01;
            if (!color)
                color = '#000';
            var con  = document.getElementById(obj).appendChild(document.createElement('canvas'));
            var ctx = con.getContext('2d');
            con.width = document.getElementById(obj).offsetWidth ? document.getElementById(obj).offsetWidth : window.innerWidth;
            con.height = document.getElementById(obj).offsetHeight ? document.getElementById(obj).offsetHeight : window.innerHeight;
            var percent = 0;
            ctx.lineWidth = 2;
            origin = [origin[0] * (con.width / 100),origin[1] * (con.height / 100)];
            function animate() {
                ctx.clearRect( 0, 0, con.width, con.height );
                for ( var i = 0; i < coordinate.length; i++ ) {
                    var x0 = coordinate[i][0][0] * (con.width / 100);
                    var y0 = coordinate[i][0][1] * (con.height / 100);
                    var x1 = coordinate[i][1][0] * (con.width / 100);
                    var y1 = coordinate[i][1][1] * (con.height / 100);
                    ctx.strokeStyle = color;
                    ctx.beginPath();
                    formula(ctx, origin, [ x0, y0 ] , [ x1, y1 ], percent);
                    ctx.stroke();
                }
                if (percent < 1)
                    percent += speed;
                else {
                    return;
                }
                requestAnimationFrame( animate );
            }

            function formula( ctx, start, end, control, percent ) {
//            二次贝塞尔曲线坐标计算公式
                var v01 = [ control[ 0 ] - start[ 0 ], control[ 1 ] - start[ 1 ] ];     // 向量<p0, p1>
                var v12 = [ end[ 0 ] - control[ 0 ], end[ 1 ] - control[ 1 ] ];     // 向量<p1, p2>
                var q0 = [ start[ 0 ] + v01[ 0 ] * percent, start[ 1 ] + v01[ 1 ] * percent ];
                var q1 = [ control[ 0 ] + v12[ 0 ] * percent, control[ 1 ] + v12[ 1 ] * percent ];
                var v = [ q1[ 0 ] - q0[ 0 ], q1[ 1 ] - q0[ 1 ] ];       // 向量<q0, q1>
                var b = [ q0[ 0 ] + v[ 0 ] * percent, q0[ 1 ] + v[ 1 ] * percent ];
                ctx.moveTo( start[ 0 ], start[ 1 ] );
                ctx.quadraticCurveTo(
                    q0[ 0 ], q0[ 1 ],
                    b[ 0 ], b[ 1 ]
                );
            }
            animate();
        }
    };


    /**
     * 全局效果说明
     * ---------------------Dividing line--------------------
     * @num_plus 数字相加
     * 参数 (obj,set_num,speed = 60)
     * 详解 obj
     *      获取容器对象
     *     set_num
     *      设置相加比例 公式: 数字 / set_num = 每次相加数字(整数)
     *     speed
     *      动画速度 默认 60ms
     * html 结构
     *      <ul>
     *          <li>*<em data-num="Number">0</em>*</li>
     *      </ul>
     *
     * ---------------------Dividing line--------------------
     * @BG_parallax 背景视差
     * 参数 (obj,speed = 0.5)
     * 详解 obj
     *      获取容器对象
     *     speed
     *      动画速度 默认 0.5
     *
     * ---------------------Dividing line--------------------
     * @Interval_Fun 区间判断
     * 参数 (obj,means,cycle,position)
     * 详解 obj
     *      获取容器对象
     *     means
     *      执行方法
     *     cycle
     *      是否多次执行  boolean值
     *     position
     *      当容器自身多少出现在屏幕中时执行 公式：容器高度 / position
     *
     * ---------------------Dividing line--------------------
     * @Video_open 视频弹窗
     * 参数 (obj,url,btn,img)
     * 详解 obj
     *       获取点击对象 ( html标签 data-src="视频地址" data-img="video第一帧" data-btn="关闭按钮颜色")
     *      method
     *       切换模式 (video / iframe) false / true
     *
     * ---------------------Dividing line--------------------
     * @Line_curve 画曲线
     * 参数 (obj,origin,coordinate,color,speed)
     * 详解 obj
     *       获取ID属性
     *     origin
     *       中心点 （数组） 百分比
     *     coordinate
     *       其它点 （控制点+终点）
     *       [
     *          [[终点],[控制点]] 百分比
     *       ]
     *     color
     *       线条颜色
     *     speed
     *       速度
     */



})